package core;

import org.json.JSONObject;

import java.io.FileReader;
import java.io.IOException;

public class Config {
    private RunType runType;
    private String host;
    private int port;
    public void load(String filename) {
        try (FileReader fr = new FileReader(filename)) {
            StringBuilder sb = new StringBuilder();
            int ch;
            while (-1 != (ch = fr.read())) {
                sb.append((char) ch);
            }
            JSONObject config = new JSONObject(sb.toString());
            populate(config);
        } catch (IOException ignored) {
        }
    }

    private void populate(JSONObject config) {
        String runType = config.getString("run_type");
        switch (runType) {
            case "server": {
                this.runType = RunType.Server;
                break;
            }
            case "client": {
                this.runType = RunType.Client;
                break;
            }
        }
        this.host = config.getString("addr");
        this.port = config.getInt("port");
    }


    public RunType getRunType() {
        return runType;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public enum RunType {
        Server("Server", 0), Client("Client", 1);
        private final String desc;
        private final Integer val;

        RunType(String desc, Integer val) {
            this.desc = desc;
            this.val = val;
        }

        public String getDesc() {
            return desc;
        }

        public Integer getVal() {
            return val;
        }
    }
}
